package com.eshealthy.controller;

import com.eshealthy.entity.*;
import com.eshealthy.service.AdminService;
import com.eshealthy.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("admin")
@Slf4j
public class AdminController {
    @Autowired
    private AdminService adminService;
    /*
     * 学习库--器械*/

    @GetMapping("deviceList")
    public Map<String,Object> findAllDevice(Integer page,Integer rows) throws IOException {
        page = page == null ? 1 : page;
        rows = rows == null ? 4 : rows;
        HashMap<String, Object> map = new HashMap<>();
        List<MedicalDevice> devices = adminService.findAllDevice(page, rows);
        Integer totals = (int)adminService.findTotalsDevice();
//        System.out.println(totals);
        Integer totalsPage = totals % rows == 0 ? totals / rows : totals / rows + 1;
        map.put("devices", devices);
        map.put("totals", totals);
        map.put("totalPage", totalsPage);
        map.put("page", page);
        return map;
    }
  /*
  * 学习库--药物*/

    @GetMapping("drugList")
    public Map<String,Object> findAllDrug(Integer page,Integer rows) throws IOException {
        page = page == null ? 1 : page;
        rows = rows == null ? 4 : rows;
        HashMap<String, Object> map = new HashMap<>();
        List<Drug> drugs = adminService.findAllDrug(page, rows);
        Integer totals = (int)adminService.findTotalsDrug();
//        System.out.println(totals);
        Integer totalsPage = totals % rows == 0 ? totals / rows : totals / rows + 1;
        map.put("drugs", drugs);
        map.put("totals", totals);
        map.put("totalPage", totalsPage);
        map.put("page", page);
        return map;
    }
    /*
     * 修改患者信息*/
    @PostMapping("updatePatient")
    public Map<String,Object>updatePatient(Patient patient){
        log.info("患者信息：[{}]",patient.toString());
        Map<String,Object>map = new HashMap<>();
        try {
            adminService.savePatient(patient);
            map.put("state",true);
            map.put("msg","修改成功");
        }catch(Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","修改失败");
        }
        return map;
    }
    /*
     * 查询患者信息*/
    @GetMapping("findPatient")
    public  Patient findPatient(String id){
        log.info("查询患者信息的id:[{}]",id);
        return adminService.findPatient(id);
    }
    /*
    /*
     * 修改科室信息*/
    @PostMapping("updateDepartment")
    public Map<String,Object>updateDepartment(Department department){
        log.info("科室信息：[{}]",department.toString());
        Map<String,Object>map = new HashMap<>();
        try {
            adminService.saveDepartment(department);
            map.put("state",true);
            map.put("msg","修改成功");
        }catch(Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","修改失败");
        }
        return map;
    }
    /*
     * 查询科室信息*/
    @GetMapping("findDepartment")
    public  Department findDepartment(String id){
        log.info("查询科室信息的id:[{}]",id);
        return adminService.findDepartment(id);
    }
    /*
    * 修改医生信息*/
    @PostMapping("updateDoctor")
    public Map<String,Object>updateDoctor(Doctor doctor){
        log.info("医生信息：[{}]",doctor.toString());
        Map<String,Object>map = new HashMap<>();
        try {
            adminService.saveDoctor(doctor);
            map.put("state",true);
            map.put("msg","修改成功");
        }catch(Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","修改失败");
        }
        return map;
    }
    /*
    * 查询医生信息*/
    @GetMapping("findDoctor")
    public  Doctor findDoctor(String id){
        log.info("查询医生信息的id:[{}]"+id);
        return adminService.findDoctor(id);
    }
    /*
    * 删除医生*/
    @GetMapping("deleteDoctor")
    public Map<String,Object>deleteDoctor(String id){
        log.info("删除医生id:[{}]"+id);
        Map<String,Object>map=new HashMap<>();
        try{
            adminService.deleteDoctor(id);
            adminService.deleteDoctorArea(id);
            map.put("state",true);
            map.put("msg","删除医生成功");
        }catch(Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","删除失败");
        }
        return map;
    }
    /*
    * 删除患者*/
    @GetMapping("deletePatient")
    public Map<String,Object>deletePatient(String id){
        log.info("删除医生id:[{}]"+id);
        Map<String,Object>map=new HashMap<>();
        try{
            adminService.deletePatient(id);
            map.put("state",true);
            map.put("msg","删除医生成功");
        }catch(Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","删除失败");
        }
        return map;
    }
    /*
     * 删除科室*/
    @GetMapping("deleteDepartment")
    public Map<String,Object>deleteDepartment(String id){
        log.info("删除科室id:[{}]"+id);
        Map<String,Object>map=new HashMap<>();
        try{
            adminService.deleteDepartment(id);
            map.put("state",true);
            map.put("msg","删除医生成功");
        }catch(Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","删除失败");
        }
        return map;
    }
    /*
     * 查询所有科室信息
     * */
    @GetMapping("findAllDepartment")
    public Map<String,Object> findAllDepartment(Integer page,Integer rows) {
        page = page==null?1:page;
        rows = rows==null?7:rows;
        HashMap<String,Object> map=new HashMap<>();
        List<Department> departments= adminService.findAllDepartment(page, rows);
        Integer totals=adminService.findTotalsDepartment();
        Integer totalsPage=totals%rows==0?totals/rows:totals/rows+1;
        map.put("departments",departments);
        map.put("totals",totals);
        map.put("totalPage",totalsPage);
        map.put("page",page);
        return map;
    }
    /*
    * 查询所有患者信息
    * */
    @GetMapping("findAllPatient")
    public Map<String,Object> findAllPatient(Integer page,Integer rows) {
        page = page==null?1:page;
        rows = rows==null?5:rows;
        System.out.println(page.getClass().toString());
        HashMap<String,Object> map=new HashMap<>();
        List<Patient> patients= adminService.findAllPatient(page, rows);
        Integer totals=adminService.findTotalsPatient();
        Integer totalsPage=totals%rows==0?totals/rows:totals/rows+1;
        map.put("patients",patients);
        map.put("totals",totals);
        map.put("totalPage",totalsPage);
        map.put("page",page);
        return map;
    }
    /*
     * 查询所有医生信息
     * */

    @GetMapping("findAllDoctor")
    public Map<String,Object> findAll(Integer page,Integer rows) {
        page = page==null?1:page;
        rows = rows==null?4:rows;
        HashMap<String,Object> map=new HashMap<>();
        List<Doctor> doctors= adminService.findAllDoctor(page, rows);
        map.put("status",true);
        map.put("msg","请求成功");
        Integer totals=adminService.findTotalsDoctor();
        Integer totalsPage=totals%rows==0?totals/rows:totals/rows+1;
        map.put("doctors",doctors);
        map.put("totals",totals);
        map.put("totalPage",totalsPage);
        map.put("page",page);
        return map;
    }
    /*
登录
    */
    @PostMapping("login")
    public Map<String, Object> login(@RequestBody Manager manager){
        log.info("当前登录的用户信息："+manager.toString());

        Map<String, Object>map = new HashMap<>();
        try{
            Manager managerDB =adminService.login(manager);
            Map<String,String> payload=new HashMap<>();
            payload.put("id",managerDB.getId());
            payload.put("name",managerDB.getName());
            String token= JWTUtils.getToken(payload);
            managerDB.setName("");
            managerDB.setPassword("");
            map.put("state",true);
            map.put("msg","用户登录成功");
            map.put("manager",managerDB);
            map.put("token",token);
        }catch(Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","输入错误，请重试");
        }
        return map;
    }

    /*
     * 注册
     * */
    @PostMapping("registerDepartment")
    public Map<String,Object> register(@RequestBody Department department, String code, HttpServletRequest request){
        log.info("用户输入的信息[{}]"+department.toString());
        log.info("用户输入的验证码信息[{}]",code);
        Map<String,Object> map = new HashMap<>();
        try{
            String key = (String) request.getServletContext().getAttribute("code"); //正确验证码
            if(key.equalsIgnoreCase(code)){
                if(department.getName()==null||department.getArea()==null||department.getLocation()==null){
                    throw new RuntimeException("输入错误");
                }else {
                    adminService.registerDepartment(department);
                    map.put("state",true);
                    map.put("msg","注册成功");
                }

            }else{
                throw new RuntimeException("验证码错误");
            }
        }
        catch(Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","提示："+e.getMessage());
        }
        return map;
    }
    @PostMapping("registerDrug")
    public Map<String,Object> registerDrug(@RequestBody Drug drug, String code, HttpServletRequest request){
        log.info("要注册的药物信息"+drug.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            String key = (String) request.getServletContext().getAttribute("code"); //正确验证码
            if(key.equalsIgnoreCase(code)){
                if(drug.getName()==null||drug.getDirections()==null||drug.getUseage()==null||drug.getTelspecification()==null){
                    throw new RuntimeException("输入错误");
                }else {
                    adminService.registerDrug(drug);
                    map.put("state",true);
                    map.put("msg","注册成功");
                }

            }else{
                throw new RuntimeException("验证码错误");
            }
        }catch(Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","提示："+e.getMessage());
        }
        return map;
    }
    @PostMapping("registerDevice")
    public Map<String,Object> registerDevice(@RequestBody MedicalDevice device, String code, HttpServletRequest request){
        log.info("要注册的器械信息"+device.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            String key = (String) request.getServletContext().getAttribute("code"); //正确验证码
            if(key.equalsIgnoreCase(code)){
                System.out.println(device.toString().length());
                if(device.getName()==null||device.getDirections()==null||device.getTelspecification()==null||device.getUseage()==null){
                    throw new RuntimeException("输入错误");
                }else {
                    adminService.registerDevice(device);
                    map.put("state",true);
                    map.put("msg","注册成功");
                }

            }else{
                throw new RuntimeException("验证码错误");
            }
        }catch(Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","提示："+e.getMessage());
        }
        return map;
    }
    /*
     * 注册案例信息*/
    @PostMapping("registerLibrary")
    public Map<String,Object> registerDevice(@RequestBody Library library, String code, HttpServletRequest request){
        log.info("要注册的医疗方案信息"+library.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            String key = (String) request.getServletContext().getAttribute("code"); //正确验证码
            if(key.equalsIgnoreCase(code)){
                if(library.getDirections()==null||library.getName()==null||library.getAdvice()==null){
                    throw new RuntimeException("输入错误");
                }else {
                    adminService.registerLibrary(library);
                    map.put("state",true);
                    map.put("msg","注册成功");
                }

            }else{
                throw new RuntimeException("验证码错误");
            }
        }catch(Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","提示："+e.getMessage());
        }
        return map;
    }
    /*
     * 注册
     * */
    @PostMapping("register")
    public Map<String,Object> register(@RequestBody Manager manager, String code, HttpServletRequest request){
        log.info("用户输入的信息[{}]"+manager.toString());
        log.info("用户输入的验证码信息[{}]"+code);
        Map<String,Object> map = new HashMap<>();
        try{
            String key = (String) request.getServletContext().getAttribute("code"); //正确验证码
            if(key.equalsIgnoreCase(code)){
                adminService.register(manager);
                adminService.registerArea(manager.getArea());
                map.put("state",true);
                map.put("msg","注册成功");
            }else{
                throw new RuntimeException("验证码错误");
            }
        }
        catch(Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","提示："+e.getMessage());
        }
        return map;
    }
}
